import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:overload_guard/app_theme.dart';
import 'package:overload_guard/main.dart';
import 'package:overload_guard/pages/data_detail_page.dart';
import 'package:overload_guard/services/api_service.dart';
import 'package:overload_guard/services/db_service.dart';
import 'package:overload_guard/widgets/buttons.dart';
import 'package:overload_guard/widgets/dialogs.dart';

class DataListPage extends StatefulWidget {
  @override
  State<DataListPage> createState() => _DataListPageState();
}

class _DataListPageState extends State<DataListPage> {
  late List dataList;

  Future<dynamic> _initPage() async {
    var result = await APIService.shippingQueryCurrentRecord(
        await DBService.getData(DBKey.userId));
    dataList = result;
    return result;
  }

  Future refreshData() async {
    var result = await APIService.shippingQueryCurrentRecord(
        await DBService.getData(DBKey.userId));
    dataList = result;
    setState(() {

    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("选择超偏载检测车板", style: TextStyle(color: Colors.white)),
        backgroundColor: AppTheme.primaryColor,
        centerTitle: true,
        leading: IconButton(
          icon: Icon(
            Icons.arrow_back,
            color: Colors.white,
          ),
          onPressed: () {
            Get.back();
          },
        ),
      ),
      backgroundColor: Color(0xFFEDEDED),
      body: FutureBuilder(
        future: _initPage(),
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            print('加载完成');
            return snapshot.data.isNotEmpty
                ? ListView.builder(
                    itemCount: dataList.length,
                    itemBuilder: (context, index) {
                      return Column(
                        children: [
                          Padding(padding: EdgeInsets.only(top: 5)),
                          Container(
                            color: Colors.white,
                            child: ListTile(
                              title: Text("车板：${dataList[index]}"),
                              trailing: Container(
                                decoration: BoxDecoration(
                                  borderRadius:
                                      BorderRadius.circular(10), // 圆角半径
                                  color: Color(0xFF53B7AD), // 容器的背景颜色
                                ),
                                child: Padding(
                                  padding: EdgeInsets.only(left: 8, right: 8),
                                  child: Text(
                                    "检测",
                                    style: TextStyle(
                                        color: Colors.white, fontSize: 15),
                                  ),
                                ),
                              ),
                              onTap: () {
                                Get.to(DataDetailPage(
                                    templateNum: dataList[index]))!.then((value) =>value == true? refreshData():null);
                              },
                            ),
                          ),
                          dataList[index] == dataList.last
                              ? Padding(
                                  padding: EdgeInsets.only(
                                      top: 10, left: 10, right: 10),
                                  child: mainSubmitButton("运输结束", () async {

                                    Get.defaultDialog(
                                        title: "警告",
                                        textConfirm: "确认",
                                        textCancel: "取消",
                                        content: Text("确认运输结束？"),
                                        onConfirm: () async {
                                          var result =
                                          await APIService.shippingFinish(
                                              await DBService.getData(
                                                  DBKey.userId));

                                          if (result != null) {
                                            Get.offAll(MyHomePage());
                                            Dialogs.showSuccess();
                                          }
                                        });

                                  }, context, AppTheme.primaryColor),
                                )
                              : Container()
                        ],
                      );
                    })
                : Center(
                    child: Text("暂无数据"),
                  );
          } else if (snapshot.hasError) {
            return Text('报错了！');
          } else {
            print('加载中');
            return Center(child: CircularProgressIndicator());
          }
        },
      ),
    );
  }
}
